<?php
namespace app\admin\controller;
use app\model\WxAssistant as WxAssistantModel;
use think\Db;


class WxAssistant extends Base
{
    public function __construct(){
        $model = new WxAssistantModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;
        $categorys = Db::name('category')->select();
        $categorys = arrayParamExtract($categorys,'id','name');

        $fields = [
            'category_id'=>['name'=>'分类','type'=>'select1','params'=>$categorys],
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'type'=>['name'=>'类型','type'=>'select1','params'=>$model->getTypeList()],
            'image'=>['name'=>'图片','type'=>'file'],
            'description'=>['name'=>'描述','type'=>'textarea'],
            'greet'=>['name'=>'欢迎语','type'=>'textarea'],
            'is_config_select'=>['name'=>'是否多模型','type'=>'select1','params'=>$model->getCommonIsList()],
            'request_type'=>['name'=>'请求类型','type'=>'select1','params'=>$model->getRequestTypeList()],
            'is_show'=>['name'=>'是否显示','type'=>'select1','params'=>$model->getCommonIsList()],
            'api_url'=>['name'=>'API地址','type'=>'text'],
            'system'=>['name'=>'模型人设，例如，你是xxx公司制作的AI助手','type'=>'textarea'],
            'api_param'=>['name'=>'API参数','type'=>'textarea'],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
                ['key'=>'type','name'=>'类型','type'=>'select1_1','params'=>$model->getTypeList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
            ['key'=>'type','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['type'] = $model->getTypeCn($vo['type']);
            $list['data'][$k]['request_type'] = $model->getRequestTypeCn($vo['request_type']);
            $list['data'][$k]['category_name'] = Db::name('category')->where('id',$vo['category_id'])->value('name');
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','type','image','category_id'],
                'nomust'=>['description','request_type','greet','is_show','api_url','api_param','system','is_config_select']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','type','image','category_id'],
                'nomust'=>['description','request_type','greet','is_show','api_url','api_param','system','is_config_select']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::updateData($id,$save_data);
    }




   
}
